<?php
namespace jy_wuliu;
defined('BY_JYA') or exit('error');
/*
	工具类
*/
class model extends \plugin{
	public function date($time){
		$res=array(
			'day'=>0,
			'hour'=>0,
			'minute'=>0,
			'second'=>0,
		);
		
		if($time<=0){
			return $res;
		}
		$res['day']=intval($time/86400);
		$time=$time-$res['day']*86400;
		$res['hour']=intval($time/3600);
		$time=$time-$res['hour']*3600;
		$res['minute']=intval($time/60);
		$res['second']=$time-$res['minute']*60;
		return $res;
	}
	public function add_member($pid=0,$formuid=0,$snsid=0,$quanid=0,$pcd='') {
		$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
		if(empty($member)){
			cfc('core')->checklogin('jy_wuliu/myhome/');
		}
		$user=pdo_get('jy_wuliu_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','title','logo'));
		if(!$user['id']){
			$insert=array(
				'mid'=>$_SESSION['uid'],
				'uniacid'=>$_SESSION['uniacid'],
				'pid'=>intval($pid),
				'quanid'=>intval($quanid),
				'snsid'=>intval($snsid),
				'formuid'=>intval($formuid),
				'title'=>$member['nickname'],
				'logo'=>$member['avatar'],
				'desc'=>'大家好！欢迎浏览我的物流圈,里面记录了我的一些货运物流供求信息！',
				'pcd'=>$pcd,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_wuliu_member',$insert);
		}elseif(!$user['title']){
			pdo_update('jy_wuliu_member',array('title'=>$member['nickname'],'logo'=>$member['avatar']),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		}
	}
	public function upitemlook($pid,$fromuser=0){
		$islook=pdo_get('jy_wuliu_looklog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$pid));
		$snsid=pdo_getcolumn('jy_wuliu_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$pid),'snsid');
		if(empty($islook)){
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'pid'=>$pid,
				'snsid'=>$snsid,
				'mid'=>$_SESSION['uid'],
				'fromuser'=>$fromuser,
				'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_wuliu_looklog',$insert);
		}else{
			if($islook['createtime']+6*3600<time()){
				pdo_update('jy_wuliu_looklog',array('looks +='=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
			}else{
				pdo_update('jy_wuliu_looklog',array('createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
			}
		}
	}
	public function upsnslook($snsid,$fromuser=0){
		$islook=pdo_get('jy_wuliu_looklog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'snsid'=>$snsid));
		if(empty($islook)){
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'snsid'=>$snsid,
				'mid'=>$_SESSION['uid'],
				'fromuser'=>$fromuser,
				'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_wuliu_looklog',$insert);
		}else{
			if($islook['createtime']+6*3600<time()){
				pdo_update('jy_wuliu_looklog',array('looks +='=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'id'=>$islook['id']));
			}else{
				pdo_update('jy_wuliu_looklog',array('createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'id'=>$islook['id']));
			}
		}
	}
}